package com.orange.agent.rebangTody.consumer;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.orange.agent.rebangTody.model.RebangDataReq;
import com.orange.agent.rebangTody.model.RebangDataRes;
import com.orange.agent.rebangTody.model.RebangSuperReq;
import com.orange.common.base.entity.ConsumerDto;
import com.orange.common.base.entity.RequestData;
import com.orange.common.base.entity.ResHeader;
import com.orange.common.enums.ResponseStatus;
import com.orange.common.utils.ConvertUtils;
import com.orange.framework.consumer.Consumer;
import com.orange.framework.consumer.annotation.HttpMapping;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeoutException;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/9/3 15:20
 */
@Slf4j
public abstract class AbstractRebangConsumer implements Consumer {

    @Override
    public ConsumerDto communicate(HttpMapping mapping, Object serverBody, RequestData<?> requestData, Class returnType) throws TimeoutException {
        ConsumerDto consumerDto = new ConsumerDto();
        try {
            String response = "";
            String transCode = mapping.transCode();
            RebangDataReq request = this.initParams(serverBody, requestData);
            //判断不是挡板
            if (isDefaultBody()) {
                log.info("挡板模式");
            } else {
                response = this.dataCommit(transCode, request);
                if (response == null) {
                    throw new TimeoutException(StrUtil.format("[{}]请求超时，请稍后再试"));
                }
            }
            JSONObject resDataJSON = JSON.parseObject(response);
            RebangDataRes rebangDataRes = ConvertUtils.convertToObject(resDataJSON, RebangDataRes.class);
            Object dataApiRes =  ConvertUtils.jsonToObject(resDataJSON.getString("data"), returnType);
            consumerDto.setServerReq(requestData.getBody());
            consumerDto.setServerRes(rebangDataRes);
            consumerDto.setBody(dataApiRes);
            consumerDto.setHeader(this.setHeader(rebangDataRes));
            consumerDto.setStatus(this.status(rebangDataRes));
        } catch (Exception e) {
            log.error("Rebang远程请求出错了:{}", e.getMessage());
        }
        return consumerDto;
    }

    private ResHeader setHeader(RebangDataRes rebangSuperRes) {
        ResHeader resHeader = new ResHeader();
        resHeader.setReturnCode(String.valueOf(rebangSuperRes.getCode()));
        resHeader.setReturnMsg(rebangSuperRes.getMsg());
        return resHeader;
    }

    private ResponseStatus status(RebangDataRes rebangSuperRes) {
        return rebangSuperRes.getCode() == 200 ? ResponseStatus.SUCCESS : ResponseStatus.FAILD;
    }

    private RebangDataReq initParams(Object serverBody, RequestData<?> requestData) {
        RebangDataReq<RebangSuperReq> rebangSuperReq = new RebangDataReq<>();
        RebangSuperReq superReq = (RebangSuperReq) serverBody;
        rebangSuperReq.setData(superReq);
        return rebangSuperReq;
    }

    protected boolean isDefaultBody() {
        return false;
    }

    protected abstract String dataCommit(String transCode, RebangDataReq<RebangSuperReq> request);

}
